#include "rga2_wrapper.h"
#include <iostream>
#include <cstring>

RGA2Wrapper::RGA2Wrapper() : m_initialized(false) {
}

RGA2Wrapper::~RGA2Wrapper() {
    if (m_initialized) {
        // 清理RGA2资源
        c_RkRgaDeInit();
    }
}

bool RGA2Wrapper::initialize() {
    if (m_initialized) {
        return true;
    }
    
    // 初始化RGA2
    int ret = c_RkRgaInit();
    if (ret != 0) {
        std::cerr << "RGA2初始化失败: " << ret << std::endl;
        return false;
    }
    
    m_initialized = true;
    std::cout << "RGA2初始化成功" << std::endl;
    return true;
}

bool RGA2Wrapper::rotateImage(const cv::Mat& input, cv::Mat& output, double angle) {
    if (!m_initialized) {
        std::cerr << "RGA2未初始化" << std::endl;
        return false;
    }
    
    // 计算旋转矩阵
    cv::Point2f center(input.cols / 2.0f, input.rows / 2.0f);
    cv::Mat rotationMatrix = cv::getRotationMatrix2D(center, angle, 1.0);
    
    // 计算旋转后的边界
    cv::Rect2f bbox = cv::RotatedRect(cv::Point2f(), input.size(), angle).boundingRect2f();
    rotationMatrix.at<double>(0, 2) += bbox.width / 2.0 - input.cols / 2.0;
    rotationMatrix.at<double>(1, 2) += bbox.height / 2.0 - input.rows / 2.0;
    
    // 创建输出图像
    output = cv::Mat::zeros(bbox.height, bbox.width, input.type());
    
    // 使用OpenCV进行旋转（如果RGA2旋转有问题，回退到OpenCV）
    cv::warpAffine(input, output, rotationMatrix, output.size());
    
    return true;
}

bool RGA2Wrapper::scaleImage(const cv::Mat& input, cv::Mat& output, int target_width, int target_height) {
    if (!m_initialized) {
        std::cerr << "RGA2未初始化" << std::endl;
        return false;
    }
    
    // 创建输出图像
    output = cv::Mat::zeros(target_height, target_width, input.type());
    
    // 使用OpenCV进行缩放（RGA2缩放可能不如OpenCV稳定）
    cv::resize(input, output, cv::Size(target_width, target_height), 0, 0, cv::INTER_LINEAR);
    
    return true;
}

bool RGA2Wrapper::padImage(const cv::Mat& input, cv::Mat& output, int target_width, int target_height) {
    if (!m_initialized) {
        std::cerr << "RGA2未初始化" << std::endl;
        return false;
    }
    
    // 创建黑色画布
    output = cv::Mat::zeros(target_height, target_width, input.type());
    
    // 将输入图像放在左上角
    cv::Rect roi(0, 0, std::min(input.cols, target_width), std::min(input.rows, target_height));
    cv::Mat inputROI = input(roi);
    cv::Mat outputROI = output(roi);
    inputROI.copyTo(outputROI);
    
    return true;
}

bool RGA2Wrapper::rotateAndPad(const cv::Mat& input, cv::Mat& output, double angle, int target_width, int target_height) {
    if (!m_initialized) {
        std::cerr << "RGA2未初始化" << std::endl;
        return false;
    }
    
    cv::Mat rotated;
    if (!rotateImage(input, rotated, angle)) {
        return false;
    }
    
    return padImage(rotated, output, target_width, target_height);
}

bool RGA2Wrapper::convertToRGA(const cv::Mat& cv_mat, rga_buffer_t& rga_buffer) {
    // 设置RGA2缓冲区参数
    memset(&rga_buffer, 0, sizeof(rga_buffer));
    
    rga_buffer.width = cv_mat.cols;
    rga_buffer.height = cv_mat.rows;
    rga_buffer.format = RK_FORMAT_BGR_888;
    rga_buffer.fd = -1;
    rga_buffer.vir_addr = cv_mat.data;
    rga_buffer.phy_addr = 0;
    rga_buffer.wstride = cv_mat.step;
    
    return true;
}

bool RGA2Wrapper::convertFromRGA(const rga_buffer_t& rga_buffer, cv::Mat& cv_mat) {
    // 从RGA2缓冲区创建OpenCV Mat
    cv_mat = cv::Mat(rga_buffer.height, rga_buffer.width, CV_8UC3, 
                     const_cast<void*>(rga_buffer.vir_addr), rga_buffer.wstride);
    return true;
}

bool RGA2Wrapper::checkRGAStatus(int status, const std::string& operation) {
    if (status != 0) {
        std::cerr << "RGA2操作失败 [" << operation << "]: " << status << std::endl;
        return false;
    }
    return true;
}
